package com.logistics.system.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.logistics.common.core.constant.Constants;
import com.logistics.system.domain.Contracts;
import com.logistics.system.service.IContractsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.logistics.common.log.annotation.Log;
import com.logistics.common.log.enums.BusinessType;
import com.logistics.common.security.annotation.RequiresPermissions;
import com.logistics.system.domain.TransportPlans;
import com.logistics.system.service.ITransportPlansService;
import com.logistics.common.core.web.controller.BaseController;
import com.logistics.common.core.web.domain.AjaxResult;
import com.logistics.common.core.utils.poi.ExcelUtil;
import com.logistics.common.core.web.page.TableDataInfo;

/**
 * 运输计划Controller
 *
 * @author logistics
 * @date 2024-06-23
 */
@RestController
@RequestMapping("/plans")
public class TransportPlansController extends BaseController
{
    @Autowired
    private ITransportPlansService transportPlansService;

    @Autowired
    private IContractsService contractsService;

    /**
     * 查询运输计划列表
     */
    @RequiresPermissions("system:plans:list")
    @GetMapping("/list")
    public TableDataInfo list(TransportPlans transportPlans)
    {
        startPage();
        List<TransportPlans> list = transportPlansService.selectTransportPlansList(transportPlans);
        return getDataTable(list);
    }

    /**
     * 导出运输计划列表
     */
    @RequiresPermissions("system:plans:export")
    @Log(title = "运输计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TransportPlans transportPlans)
    {
        List<TransportPlans> list = transportPlansService.selectTransportPlansList(transportPlans);
        ExcelUtil<TransportPlans> util = new ExcelUtil<TransportPlans>(TransportPlans.class);
        util.exportExcel(response, list, "运输计划数据");
    }

    /**
     * 获取运输计划详细信息
     */
    @RequiresPermissions("system:plans:query")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") Long planId)
    {
        return success(transportPlansService.selectTransportPlansByPlanId(planId));
    }

    /**
     * 新增运输计划
     */
    @RequiresPermissions("system:plans:add")
    @Log(title = "运输计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TransportPlans transportPlans)
    {
        Contracts contracts = contractsService.selectContractsByContractId(transportPlans.getContractId());
        if(!contracts.getContractStatus().equals(Constants.CONTRACTS_STATUS_2)){
            return AjaxResult.error("合同状态不是已签约完成，无法创建运输计划");
        }
        //合同中签订的最大运输量
        BigDecimal maxTransportVolume = contracts.getTotalTransportVolume();
        //已经生成的运输量
        BigDecimal generatedTransportVolume = transportPlansService.sumTransportVolume(transportPlans.getContractId());
        //判断本次生成计划中的计划量+已生成的计划量是否大于合同中的最大运输量
        if(generatedTransportVolume.add(transportPlans.getPlannedTransportVolume()).compareTo(maxTransportVolume) >= 0){
            return AjaxResult.error("运输量超过合同中的最大运输量，请修改");
        }
        contractsService.updateContracts(contracts);
        return toAjax(transportPlansService.insertTransportPlans(transportPlans));
    }

    /**
     * 修改运输计划
     */
    @RequiresPermissions("system:plans:edit")
    @Log(title = "运输计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TransportPlans transportPlans)
    {
        return toAjax(transportPlansService.updateTransportPlans(transportPlans));
    }

    /**
     * 删除运输计划
     */
    @RequiresPermissions("system:plans:remove")
    @Log(title = "运输计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds)
    {
        return toAjax(transportPlansService.deleteTransportPlansByPlanIds(planIds));
    }
}
